﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;

namespace MidSoft.ProjectInfo
{
    public partial class ProjectList : System.Web.UI.Page
    {
        ChinaSoft.BLL.BLL_tb_MidSoft_CareProject bll = new ChinaSoft.BLL.BLL_tb_MidSoft_CareProject();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void imgBtnSelect_Click(object sender, ImageClickEventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            string[] strId=idlist.Split(',');
            ChinaSoft.Model.Model_tb_MidSoft_CareProject carePm = new ChinaSoft.Model.Model_tb_MidSoft_CareProject();
            for(int i=0;i<strId.Length;i++)
            {
                if (bll.GetList("ProjectInfoId=" + strId[i]).Tables[0].Rows.Count < 1)
                {
                    carePm.ProjectInfoId = Convert.ToInt16(strId[i]);
                    carePm.CreateTime = System.DateTime.Now;
                    carePm.CreateUserId = 5;
                    bll.Add(carePm);
                }
            }
            #region 设为关注操作日志
            ChinaSoft.BLL.BLL_tb_MidSoft_log logMgr = new ChinaSoft.BLL.BLL_tb_MidSoft_log();
            ChinaSoft.Model.Model_tb_MidSoft_log log = new ChinaSoft.Model.Model_tb_MidSoft_log();
            log.CreateTime = DateTime.Now;
            log.description = " 设为关注项目,ID=" + idlist;
            log.userAccount = "";
            if (Session["UserAccount"] != null && Session["UserAccount"].ToString() != "")
                log.userAccount = Session["UserAccount"].ToString();
            log.userName = "";
            if (Session["UserName"] != null && Session["UserName"].ToString() != "")
                log.userName = Session["UserName"].ToString();
            log.LogType = 3;
            int UserId = logMgr.Add(log);
            #endregion
            BindData();
            MessageBox.Show(this, "完成设置！");
        }

        #region gridView

        public void BindData()
        {
            #region
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[7].Visible = true;
            //}
            #endregion
            StringBuilder strWhere = new StringBuilder();
            if (txtProjectName.Text.Trim() != "")
            {
                strWhere.AppendFormat("ProjectName like '%{0}%'", txtProjectName.Text.Trim());
            }

            try
            {
                lblCount.Text = (new ChinaSoft.BLL.Common.Pager()).RecordCount("tb_MidSoft_ProjectInfo", strWhere.ToString()).ToString();
                ChinaSoft.BLL.Common.Pager.BindGridView(gridView, AspNetPager1, "tb_MidSoft_ProjectInfo", "ProjectInfoId", "*", strWhere.ToString(), "CreateTime desc", "");
            }
            catch (Exception ex)
            {
                Response.Write("数据库错误：" + ex.Message);
                Response.End();
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[0].Text = "<input type=\"checkbox\" id=\"cbSelectAll\" name=\"cbSelectAll\" onclick=\"javascript:checkJs(this.checked);\" />";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                //linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");

                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}
                Label lblState = (Label)e.Row.FindControl("Label1");
                switch (lblState.Text)
                {
                    case "0":
                        lblState.Text = "未完工";
                        break;
                    case "1":
                        lblState.Text = "进行中";
                        break;
                    case "2":
                        lblState.Text = "已完工";
                        break;
                    default:
                        break;
                }
            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("checkboxname");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion
    }
}